const { Menu } = require("electron");
const path = require("path");
const CreateAddServeWindow = require(path.join(
  __dirname,
  "../addServe/addServe"
));
const store = require(path.join(__dirname, "../../store/store.js"));
const addChannelWin = require(path.join(
  __dirname,
  "../addChannel/addChannel.js"
));

exports.setMenu = (parent) => {
  const template = [
    {
      label: "服务器",
      click: () => {
        const AddServeWindow = new CreateAddServeWindow(parent);
        AddServeWindow.show();
      },
    },
    {
      label: "工具",
      click: () => {
        const toolsWin = require(path.join(__dirname, "../tools/tools.js"));
        toolsWin.createToolsWin(parent).show();
      },
    },
    {
      label: "刷新(R)",
      role: "reload",
    },
    {
      label: "开发工具者",
      role: "toggleDevTools",
    },
  ];

  const menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);
};

exports.setServerMenu = (parent) => {
  const template = [
    {
      label: "加入服务器",
      label: "",
      label: "删除服务器",
    },
    {
      label: "工具",
    },
    {
      label: "刷新",
      role: "reload",
    },
    {
      label: "开发工具者",
      role: "toggleDevTools",
    },
  ];

  const menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);
};

exports.serveMenu = (parent, res) => {
  const template = [
    res.state
      ? {
          label: "添加频道",
          click: () => {
            addChannelWin.createAddChannelWin(parent).show();
          },
        }
      : null,
    {
      label: "删除服务器",
      click: () => {
        const array = store.store.serverList.filter((item) => {
          if (item.ip === res.whitch) {
            parent.send("serverListUpdata", item.ip);
          }
          return item.ip !== res.whitch;
        });
        store.set("serverList", array);
      },
    },
  ].filter((item) => item);

  const menu = Menu.buildFromTemplate(template);
  menu.popup({ window: parent });
};
